package com.dianban.controller.nengliang;

import com.dianban.base.ResultCode;
import com.dianban.base.ResultVo;
import com.dianban.config.redis.RedisConstant;
import com.dianban.config.redis.RedisUtil;
import com.dianban.controller.BaseController;
import com.dianban.entity.JwShoproArea;
import com.dianban.entity.StoreGuide;
import com.dianban.entity.User;
import com.dianban.service.JwShoproAreaService;
import com.dianban.service.StoreGuideService;
import com.dianban.utils.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Api(value = "ApiStoreGuideController", description = "门店老师控制器")
@RestController
@RequestMapping(value = "${interfacePath}/nengliang/guide/")
public class ApiStoreGuideController extends BaseController {

    @Autowired
    private StoreGuideService storeGuideService;
    @Autowired
    private JwShoproAreaService jwShoproAreaService;

    @ApiOperation(notes = "add", httpMethod = "POST", value = "门店老师入驻")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userToken", value = "用户token", required = true, paramType = "header", dataType = "string"),
            @ApiImplicitParam(name = "storeId", value = "门店Id", required = true, paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "name", value = "姓名", required = true, paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "phone", value = "手机号", required = true, paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "province", value = "省", required = true, paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "city", value = "市", required = true, paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "area", value = "区", required = true, paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "address", value = "详细地址", required = true, paramType = "query", dataType = "string"),
    })
    @RequestMapping(value = "add", method = RequestMethod.POST)
    public ResultVo add(String storeId, String name, String phone, String province, String city, String area, String address, HttpServletRequest request, HttpServletResponse response) {
        String userToken = request.getHeader(UserUtils.USER_TOKEN);
        String key = RedisConstant.USER_TOKEN_KEY + userToken;
        User user = (User) RedisUtil.get(key);
        if (user == null) {
            return ResultVo.error(ResultCode.ERROR, "请求头userToken过期");
        }
        try {
            // 查询省市区具体名称
//            JwShoproArea province = jwShoproAreaService.findById(provinceId);
//            if (province == null) {
//                return ResultVo.error(ResultCode.ERROR, "省id数据不对");
//            }
//            JwShoproArea city = jwShoproAreaService.findById(cityId);
//            if (city == null) {
//                return ResultVo.error(ResultCode.ERROR, "市id数据不对");
//            }
//            JwShoproArea area = jwShoproAreaService.findById(areaId);
//            if (area == null) {
//                return ResultVo.error(ResultCode.ERROR, "区id数据不对");
//            }
            StoreGuide _storeGuide = storeGuideService.findByUserId(user.getId());
            if (_storeGuide != null) {
                return ResultVo.error(ResultCode.ERROR, "当前用户已入驻，无需再申请");
            }
            StoreGuide storeGuide = new StoreGuide();
            storeGuide.setUserId(user.getId());
            //storeGuide.setStoreId(Long.valueOf(storeId));
            storeGuide.setName(name);
            storeGuide.setPhone(phone);
            storeGuide.setAddress(address);
            storeGuide.setProvinceName(province);
            storeGuide.setCityName(city);
            storeGuide.setAreaName(area);
            storeGuide.setStatus(0);
            storeGuideService.insert(storeGuide);
        } catch (Throwable e) {
            e.printStackTrace();
            return ResultVo.error(ResultCode.ERROR, "操作失败");
        }
        return ResultVo.success("", "操作成功");
    }

}
